/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

/**
 *
 * @author Enyths
 */
@ManagedBean
@RequestScoped
public class ArtistsBean {
    private ArrayList<Artist> tableArtists;
    private Artist selectedArtist;

    public void setSelectedArtist(Artist selectedArtist) {
        this.selectedArtist = selectedArtist;
    }

    public Artist getSelectedArtist() {
        return selectedArtist;
    }

    public ArrayList<Artist> getTableArtists() {
        return tableArtists;
    }
    
    /**
     * Creates a new instance of ArtistsBean
     */
    public ArtistsBean() {
        tableArtists = getDataFromDB();
        
    }
    
    private ArrayList<Artist> getDataFromDB() {
        ArrayList<Artist> listArtists = new ArrayList<Artist>();
        
        try  
        { 
           Context initCtx = new InitialContext();

           DataSource dataSource = (DataSource)initCtx.lookup("jdbc/DBJeemusic");

           Connection connection =  dataSource.getConnection();
           Statement requete = connection.createStatement();
           ResultSet rset;

           rset = requete.executeQuery("SELECT * FROM auteur");
           while(rset.next()) {
               listArtists.add(new Artist(Integer.parseInt(rset.getString(1)), rset.getString(2), rset.getString(3), rset.getString(4), rset.getString(5)));
           }

           connection.close();
        }
        catch (Exception ex){ex.printStackTrace();
            
        }
        
        return listArtists;
    }
}
